package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileFeign;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@Api(tags = "讲师管理模块")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j

public class AdminTeacherController {
    @Autowired
    TeacherService teacherService;

    @Autowired
    OssFileFeign ossFileFeign;
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("list")
    public R list() {
        ossFileFeign.test();
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list);
    }

    /**
     * 根据ID查询指定讲师
     */
    @ApiOperation(value = "查询指定id的讲师")
    @GetMapping("selectById/{id}")
    public R selectById(@PathVariable String id) {
        log.debug("当前的日志级别：{}，当前时间：{}", "debug", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        log.info("当前的日志级别：{}，当前时间：{}", "info", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        log.warn("当前的日志级别：{}，当前时间：{}", "warn", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        log.error("当前的日志级别：{}，当前时间：{}", "error", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        Teacher teacher = teacherService.getById(id);
//        int i = 10/0;
        return R.ok().data("item", teacher);
    }

    //根据传入的teacher对象修改teacher
    @PutMapping("update")
    @ApiOperation(value = "修改讲师信息")
    public R update(@RequestBody Teacher teacher) {
        System.out.println("teacher = " + teacher);
        String successMessage = "添加成功";
        String failMessage = "添加失败";
        teacherService.updateById(teacher);
        return R.ok().message(successMessage);
    }

    //查询所有的讲师，返回分页过滤后的数据
    @ApiOperation(value = "查询所有讲师（带页码）")
    @GetMapping("selectAll/{pageNum}/{pageSize}")
    public R selectAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize, TeacherQuery teacherQuery) {
        Page<Teacher> page = new Page<>(pageNum, pageSize);
        Page<Teacher> teacherPage = teacherService.getQuerypage(page,teacherQuery);
        return R.ok().data("page", teacherPage);
    }

    //新增指定讲师
    @PostMapping("insert")
    @ApiOperation(value = "新增讲师")
    public R insertTeacher(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return R.ok();

    }

    //删除指定讲师
    @DeleteMapping("deleteById/{id}")
    @ApiOperation(value = "删除指定id的讲师")
    public R delteById(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        ossFileFeign.delete(teacher.getAvatar());
        teacherService.removeById(id);
        return R.ok();

    }
    //根据Id列表删除讲师
    @DeleteMapping("deleteByIds")
    @ApiOperation(value = "根据id列表删除指定的讲师")
    public R deleteByids(@RequestBody List<String> ids){
        teacherService.removeByIds(ids);
        return R.ok();
    }

    @GetMapping("test")
    @ApiOperation(value = "远程测试服务调用")
    public  R testRemote(){
       return ossFileFeign.test();
    }
}

